
* Set Working Directory
cd "C:\Users\14258\Dropbox\Projects\Rural Economic Ideology\Analysis\Replication Data"

* Be sure directory includes the following:
* Munis_2018CES_module.dta
* 2018CES.dta (not in Dataverse, publicly available)
* anes_pilot_2019.dta (not in Dataverse, publicly available)
* Note: Lunz Trujillo and Crowley (2022) data not in Dataverse - contact Lunz Trujillo for permission.

**************************************
***** CES 2018 (from Munis 2020) *****
**************************************

* Merge Munis Module to 2018 CES
use Munis_2018CES_module.dta, clear
merge 1:1 caseid using 2018CES.dta
drop if _merge !=3
drop if place3 != 3 // drop non-rural

* Rural Consciousness (Resentment)
gen ruralres = cces1
egen standardized_ruralconsc = std(ruralres)
sum standardized_ruralconsc, det

* Demographics 
replace age = 1-(birthyr-1932)/68

gen female = .
replace female = 1 if gender == 2
replace female = 0 if gender == 1

replace white = 0 
replace white = 1 if race == 1 & hispanic == 0

gen black = 0
replace black = 1 if race == 2 & hispanic == 0

replace hispanic = 1 if race == 3

gen otherrace = 0
replace otherrace = 1 if white == 0 & black == 0 & hispanic == 0 

drop race
gen race = 1 if white == 1
replace race = 2 if black == 1
replace race = 3 if hispanic == 1
replace race = 4 if otherrace == 1

gen education = .
replace education = (educ-1)/5

gen income = .
replace income = (faminc-1)/13
replace income = . if faminc == 97

gen unemployed = 0 
replace unemployed = 1 if employ == 3 | employ == 4

gen married = 0
replace married = 1 if marstat == 1 | marstat == 6

gen parent = 0
replace parent = 1 if child18 == 1

gen union_new = 0
replace union_new = 1 if union == 1
replace union_new = 1 if union_coverage == 1

gen religion = 1-(pew_religimp-1)/3

gen church = 1-(pew_churatd-1)/5 if pew_churatd < 7

* Political Engagement
gen interest = 1-(newsint-1)/3 if newsint < 5

gen know1 = 0
replace know1 = 1 if CC18_309a == 1
gen know2 = 0 
replace know2 = 1 if CC18_309b == 1
alpha know1-know2, gen(knowledge)

alpha interest knowledge, gen(engagement)

* Economic Policy Preferences
gen welfare = .
replace welfare = 1-(CC18_426_1-1)/4
gen corptaxcut = 0 if CC18_325a == 1
replace corptaxcut = 1 if CC18_325a == 2
gen incometax_high = 0 if CC18_325f_new == 1
replace incometax_high = 1 if CC18_325f_new == 2
gen med4all = 0 if CC18_327a == 2
replace med4all = 1 if CC18_327a == 1
gen obamacare1 = 0 if CC18_327c == 1
replace obamacare1 = 1 if CC18_327c == 2
gen obamacare2 = 0 if CC18_327d == 1
replace obamacare2 = 1 if CC18_327d == 2
gen obamacare3 = 0 if CC18_327e == 1
replace obamacare3 = 1 if CC18_327e == 2
alpha obamacare1-obamacare3, gen(obamacare)
gen healthspending = 1-(CC18_426_2 -1)/4
gen educspending = 1-(CC18_426_3 -1)/4
gen minwage = 0 if CC18_414A == 2
replace minwage = 1 if CC18_414A == 1
gen taxspend1 = 0 if CC18_414B == 2
replace taxspend1 = 1 if CC18_414B == 1
gen taxspend2 = 0 if CC18_414C == 2
replace taxspend2 = 1 if CC18_414C == 1
gen incometax = 0 if CC18_414D == 1
replace incometax = 1 if CC18_414D == 2
gen bankregs = 0 if CC18_416 == 1
replace bankregs = 1 if CC18_416 == 2
gen regulations = 0 if CC18_417_e == 1
replace regulations = 1 if CC18_417_e == 2

factor welfare corptaxcut incometax_high  med4all obamacare healthspending educspending minwage taxspend1 taxspend2 incometax bankregs regulations [aweight=teamweight], pcf
alpha welfare corptaxcut incometax_high  med4all obamacare healthspending educspending minwage taxspend1 taxspend2 incometax bankregs regulations, gen(econ_liberalism)

* Trade Policy Preferences
gen antitrade1 = .
replace antitrade1 = 1 if CC18_331a == 1
replace antitrade1 = 0 if CC18_331a == 2
gen antitrade2a = .
replace antitrade2a = 1 if CC18_331b == 1
replace antitrade2a = 0 if CC18_331b == 2
gen antitrade2b = .
replace antitrade2b = 1 if CC18_331c == 1
replace antitrade2b = 0 if CC18_331c == 2
alpha antitrade2a antitrade2b, gen(antitrade2)

alpha antitrade1 antitrade2, gen(antitrade_scale)

* Partisanship and Ideology
gen ideology = .
replace ideology = CC18_334A
replace ideology = 4 if CC18_334A == 8
replace ideology = 1-(ideology-1)/6
gen ideo01 = ideology

gen pid01 = 1-(pid7-1)/6 if pid7 < 8
replace pid01 = .5 if pid7 == 8

gen trump_approval = 1-(CC18_308a-1)/3 if CC18_308a < 5

alpha trump_approval pid01 ideo01, gen(leftright_scale)

* Multiple Imputation
set seed 1996
mi set wide
mi register imputed income male white black hispanic education parent married union unemployed age religion church 
mi impute chained (logit) male white black hispanic parent married union unemployed (reg) income education age religion church, add(25)

mi svyset [pweight = teamweight]

* Figure 6
foreach dv in leftright_scale econ_liberalism antitrade_scale {
quietly mi estimate: svy: regress `dv' c.standardized_ruralconsc##c.engagement i.male##c.engagement i.white##c.engagement i.black##c.engagement i.hispanic##c.engagement c.age##c.engagement c.education##c.engagement c.income##c.engagement i.parent##c.engagement i.married##c.engagement i.union##c.engagement i.unemployed##c.engagement c.religion##c.engagement c.church##c.engagement
    quietly mimrgns, at(standardized_ruralconsc=(-1.884688 1.724145) engagement=(0(.25)1)) coeflegend post
    lincom _b[6._at]-_b[1._at]
    lincom _b[7._at]-_b[2._at]
    lincom _b[8._at]-_b[3._at]
    lincom _b[9._at]-_b[4._at]
    lincom _b[10._at]-_b[5._at]
} // Note: Omit state from the CES model since the sample is small and state fixed effects eat up too many dfs. 

* Appendix 5
mi estimate: svy: mean interest
mi estimate: svy: mean knowledge
mi estimate: svy: mean engagement


*********************************************************
***** 2019 ANES Pilot (from Nelsen and Petsko 2021) *****
*********************************************************

use anes_pilot_2019.dta, clear 

* Keep Only Rural/Small-Town
drop if placeid1a == 1 | placeid1a == 2 | placeid1a == 5
drop if placeid1b == 1 | placeid1b == 2 
drop if weight == .

* Demographics
gen age = 0-(birthyr-2000)/73

gen male = 0
replace male = 1 if gender == 1

replace race = 6 if race == 7

gen white = 0
replace white = 1 if race == 1
gen black = 0
replace black = 1 if race == 2
gen hispanic = 0
replace hispanic = 1 if race == 3
gen asian = 0
replace asian = 1 if race == 4
gen native = 0
replace native = 1 if race == 5
gen otherrace = 0
replace otherrace = 1 if race == 6

replace educ = (educ-1)/5

gen married = 0
replace married = 1 if marstat == 1

gen parent = 0
replace parent = 1 if child18 == 1

gen unemployed = 0 
replace unemployed = 1 if employ == 3 | employ == 4

gen south = 0
replace south = 1 if region == 3

gen income = (faminc_new-1)/15 if faminc_new != 97

gen religion = 1-(pew_religimp-1)/3

gen church = .
replace church = 0 if att1 == 2
replace church = 1-(att2-1)/4 if att2 > 0 

gen state = inputstate

* Rural Consciousness
gen ruralres1 = .
replace ruralres1 = 1-(rural1alt1-1)/6 if rural1alt1 > 0 
replace ruralres1 = 1-(rural1alt2-1)/6 if rural1alt2 > 0 
gen ruralres2 = .
replace ruralres2 = (rural2alt1-1)/6 if rural2alt1 > 0 
replace ruralres2 = (rural2alt2-1)/6 if rural2alt2 > 0 
gen ruralres3 = .
replace ruralres3 = (rural3alt1-1)/6 if rural3alt1 > 0 
replace ruralres3 = (rural3alt2-1)/6 if rural3alt2 > 0 
gen ruralres4 = .
replace ruralres4 = (rural4alt1-1)/6 if rural4alt1 > 0 
replace ruralres4 = (rural4alt2-1)/6 if rural4alt2 > 0 
alpha ruralres1 ruralres2 ruralres3 ruralres4, gen(ruralres) item
egen standardized_ruralconsc = std(ruralres)
sum standardized_ruralconsc, det

* Political Engagement
gen kn1 = pk_cjus_correct
gen kn2 = pk_germ_correct 
gen kn3 = 0
replace kn3 = 1 if pk_sen == 6

gen interest = 1-(newsint-1)/3 if newsint > 0 & newsint != 7

alpha kn1 kn2 kn3, gen(knowledge)
alpha knowledge interest, gen(engagement)

* Partisanship and Ideology
gen trump_general = 1-(apppres7-1)/6 if apppres7 > 0
replace trump_general = 1-(apppres5-1)/4 if apppres5 > 0
gen trump_foreign = 1-(frnpres7-1)/6 if frnpres7 > 0
replace trump_foreign = 1-(frnpres5-1)/4 if frnpres5 > 0
gen trump_imm = 1-(immpres7-1)/6 if immpres7 > 0
replace trump_imm = 1-(immpres5-1)/4 if immpres5 > 0
gen trump_econ = 1-(econpres7-1)/6 if econpres7 > 0
replace trump_econ = 1-(econpres5-1)/4 if econpres5 > 0

alpha trump_general trump_foreign trump_imm trump_econ, gen(trump_approval)
replace trump_approval=1-trump_approval

gen ft_bidentrump = (((ftbiden-fttrump)/100)+1)/2 if ftbiden >= 0 & ftbiden <= 100 & fttrump >= 0 & fttrump <= 100

gen pid01 = 1-(pid7x-1)/6 
replace pid01 = .5 if pid7x == -7

gen ideo01 = 1-(ideo5-1)/4 
replace ideo01 = .5 if ideo5 == -7 | ideo5 == 6

factor trump_approval ft_bidentrump pid01 ideo01 [aweight=weight], pcf
alpha trump_approval ft_bidentrump pid01 ideo01, gen(leftright_scale)

* Economic Policy Preferences
replace billtax = 1-(billtax-1)/6
gen inequality = 1-(richpoor2-1)/4 if richpoor2 != -7
gen ubi = 1-(guarinc-1)/6
gen med4all = 1-(hlthcare1-1)/6 if hlthcare1 != -1
gen freetuition = 1-(freecol-1)/6 if freecol != -7
gen studentloans = 1-(loans-1)/6 if loans != -7
replace prek = 1-(prek-1)/6

factor billtax inequality ubi med4all freetuition studentloans prek [aweight=weight], pcf
alpha billtax inequality ubi med4all freetuition studentloans prek, gen(econ_liberalism)

* Multiple Imputation
set seed 1996
mi set wide
mi register imputed income male white black hispanic educ parent married unemployed age religion church 
mi impute chained (logit) male white black hispanic parent married unemployed (reg) income educ age religion church, add(25)

mi svyset [pweight = weight]

* Figure 6
foreach dv in leftright_scale econ_liberalism {
quietly mi estimate: svy: regress `dv' c.standardized_ruralconsc##c.engagement i.male##c.engagement i.white##c.engagement i.black##c.engagement i.hispanic##c.engagement c.age##c.engagement c.educ##c.engagement c.income##c.engagement i.parent##c.engagement i.married##c.engagement i.unemployed##c.engagement c.religion##c.engagement c.church##c.engagement i.state
    quietly mimrgns, at(standardized_ruralconsc=(-1.455449 1.840541) engagement=(0(.25)1)) coeflegend post
    lincom _b[6._at]-_b[1._at]
    lincom _b[7._at]-_b[2._at]
    lincom _b[8._at]-_b[3._at]
    lincom _b[9._at]-_b[4._at]
    lincom _b[10._at]-_b[5._at]
}

* Appendix 5
mi estimate: svy: mean interest
mi estimate: svy: mean knowledge
mi estimate: svy: mean engagement


*****************************************************************
***** 2018/2019 Lucid (from Lunz Trujillo and Crowley 2022) *****
*****************************************************************

***** 2018 DATA *****
use 2018_Survey.dta, clear

* Sample Identifier
gen year = 2018

* Drop Non-Rural
drop if rc == .

* Fix Variables Defined by Labels
encode gender, gen(sex)

* Engagement Scale
alpha polint polknow, gen(engagement)

* Left-Right Partisan-Ideological Scale
factor trumpvote pid ideo [aweight=weight], pcf
alpha trumpvote pid ideo, gen(leftright_scale)

save 2018_Survey_Coded.dta, replace

****** 2019 DATA ******
use 2019_Survey.dta, clear

* Sample Identifier
gen year = 2019

* Drop Non-Rural
drop if rc == .

* Engagement Scale
alpha polint polknow, gen(engagement)

* Rename Variables 
gen income = inc
gen sex = gender
gen education = edu
gen trumpvote = votetrump

* Left-Right Partisan-Ideological Scale
factor trumpvote pid ideo [aweight=weight], pcf
alpha trumpvote pid ideo, gen(leftright_scale)

save 2019_Survey_Coded.dta, replace

***** MERGE DATASETS *****
append using 2018_Survey_Coded.dta, force

egen standardized_ruralconsc = std(rc)
sum standardized_ruralconsc, det

replace leftright_scale = 1-leftright_scale


* Multiple Imputation
set seed 1996
mi set wide
mi register imputed income sex white black hisp educ age 
mi impute chained (logit) sex white black hisp (reg) income educ age, add(25)

mi svyset [pweight = weight]

sum standardized_ruralconsc, det

* Figure 6
quietly mi estimate: svy: regress leftright_scale c.standardized_ruralconsc##c.engagement i.sex##c.engagement i.white##c.engagement i.black##c.engagement i.hisp##c.engagement c.age##c.engagement c.educ##c.engagement c.income##c.engagement i.year
    quietly mimrgns, at(standardized_ruralconsc=(-1.452882 1.804639) engagement=(0(.25)1)) coeflegend post
    lincom _b[6._at]-_b[1._at]
    lincom _b[7._at]-_b[2._at]
    lincom _b[8._at]-_b[3._at]
    lincom _b[9._at]-_b[4._at]
    lincom _b[10._at]-_b[5._at]

* Appendix 5
mean polint
mean polknow
mean engagement
